package com.wang.code;

import java.util.concurrent.locks.ReentrantLock;

public class ThreadTestImpl implements Runnable {
	public int num = 50;
	public ReentrantLock lock = new ReentrantLock();
	@Override
	public void run() {
		while(num > 0){
			sellTicket();
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
//	public synchronized void sellTicket(){
//		if (num > 0) {
//			System.out.println(Thread.currentThread().getName()+"当前销售的票："+num--);
//		}
//	}
//	public void sellTicket(){
//		synchronized (this) {
//			if (num > 0) {
//				System.out.println(Thread.currentThread().getName()+"当前销售的票："+num--);
//			}
//		}
//	}
	public void sellTicket(){
		lock.lock();
		try{
			if (num > 0) {
				System.out.println(Thread.currentThread().getName()+"当前销售的票："+num--);
			}
		}finally{
			lock.unlock();
		}
	}

}
